<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 第一行，搜索框和添加按钮 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index" label="#"></el-table-column>
        <!-- 商品名称 -->
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <!-- 商品价格 -->
        <el-table-column label="商品价格(元)" prop="goods_price" width="95px">
        </el-table-column>
        <!-- 商品重量 -->
        <el-table-column label="商品重量" prop="goods_weight" width="70px"></el-table-column>
        <!-- 商品创建时间 -->
        <el-table-column label="创建时间" width="140px">
           <template v-slot:default="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <!-- 操作列 -->
        <el-table-column label="操作" width="130px">
          <template v-slot:default="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="toEditGoods(scope.row.goods_id)"></el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeGoods(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
       <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total" background>
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 定义查询参数
      queryInfo: {
        query: '',
        pagesize: 10,
        pagenum: 1
      },
      // 商品列表数据
      goodsList: [],
      // 共有多少条数据
      total: 0
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表
    async getGoodsList() {
      // 发请求
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      // 处理请求结果
      if (res.meta.status !== 200) {
        // 如果失败，就提示
        return this.$message.error(res.meta.msg)
      } else {
        this.$message.success(res.meta.msg)
        // 如果成功，要保存数据
        this.goodsList = res.data.goods
        this.total = res.data.total
        console.log(res.data)
      }
    },
    // 处理页大小的改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      // 重新获取数据
      this.getGoodsList()
    },
    // 处理当前页发生改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      // 重新获取数据
      this.getGoodsList()
    },
    // 点击编辑按钮
    toEditGoods(id) {
      // 跳转到编辑页面，并且传递参数
      this.$router.push({
        path: '/goods/edit',
        query: { goods_id: id }
      })
    },
    // 点击删除按钮
    async removeGoods(row) {
      // 弹框提示用户
      const confrimResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 判断用户是否想删除
      if (confrimResult !== 'confirm') {
        // 如果不想删除
        return this.$message.info('取消删除')
      } else { // 如果想要删除
        // 发请求，删除商品
        const { data: res } = await this.$http.delete(`goods/${row.goods_id}`)
        if (res.meta.status !== 200) {
          // 如果请求出错
          return this.$message.error(res.meta.msg)
        } else {
          // 提示删除成功
          this.$message.success('删除商品成功')
          // 重新获取商品数据
          this.getGoodsList()
        }
      }
    },
    // 点击添加商品的按钮
    goAddPage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
